package sm.pzavoli.mercato.model.db;

import java.util.ArrayList;
import java.util.List;

import sm.pzavoli.mercato.MercatoDBOpenHelper;
import sm.pzavoli.mercato.model.Acquisto;
import sm.pzavoli.mercato.model.Ticker;
import sm.pzavoli.std.DMC;
import sm.pzavoli.std.Lista;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.util.Log;

public class AcquistoDAO {
	public static List<Acquisto> queryAcquisti(MercatoDBOpenHelper db, int id, 
			String orderby) {
		try {
	        if (db == null) {
	            Log.e("AcquistoDAO", "db is null");
	            return null;
	        }
	        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
	        qb.setTables(MercatoDBOpenHelper.ACQUISTI_TABLE);
	        String selection = "";
	        if ( id > 0) {
	        	selection = db.KEY_ID_ACQUISTI+" = "+id;
	        }
	        Cursor c = qb.query(db.getReadableDatabase(),
	                null,
	                selection, null,
	                null, null, db.KEY_DATE);
	        ArrayList<Acquisto> lista = new ArrayList<Acquisto>();
	        while ( c.moveToNext()) {
	        	Acquisto a = new Acquisto(c.getInt(0),c.getLong(1),c.getInt(2),c.getInt(3));
	        	lista.add(a);
	        }
	        c.close();
	        return lista;
		} catch(Exception e) {
			e.printStackTrace();
			Log.e("TicketDAO", "Errore in apertura cursore");
			return null;
		}
	}

	
	public static boolean insert(MercatoDBOpenHelper db, Acquisto acquisto) {
		try {
	        if (db == null) {
	            Log.e("AcquistoDAO", "db is null");
	            return false;
	        }
	        SQLiteDatabase database = db.getWritableDatabase();
	        ContentValues values = new ContentValues();
	        values.put(db.KEY_ID_TICKER, acquisto.getId());
	        long data = acquisto.getDateLong();
	        values.put(db.KEY_DATE, data);
	        values.put("quantity", acquisto.getQuantity());
	        values.put("price", acquisto.getPrice());
	        long id = database.insert(db.ACQUISTI_TABLE,null,values);
	        if ( id < 0) 
	        	return false;
	        return true;
		} catch(Exception e) {
			e.printStackTrace();
			Log.e("TickerDAO", "Errore in insert");
			return false;
		}
	}

	public static boolean update(MercatoDBOpenHelper db, Acquisto acquisto) {
		try {
	        if (db == null) {
	            Log.e("AcquistoDAO.update", "db is null");
	            return false;
	        }
	        SQLiteDatabase database = db.getWritableDatabase();
	        ContentValues values = new ContentValues();
	        values.put(Acquisto.CSZ_ID, acquisto.getId());
	        long data = acquisto.getDateLong();
	        values.put("quantity", acquisto.getQuantity());
	        values.put("price", acquisto.getPrice());
	        String where = ""+Acquisto.CSZ_DATE + " = "+data;
	        where += " AND " + Acquisto.CSZ_ID + " = "+acquisto.getId();
	        long id = database.update(db.ACQUISTI_TABLE,values,where,null);
	        if ( id < 0) 
	        	return false;
	        return true;
		} catch(Exception e) {
			e.printStackTrace();
			Log.e("TickerDAO", "Errore in insert");
			return false;
		}
	}
	
	public static boolean delete(MercatoDBOpenHelper db, Acquisto acquisto, Ticker ticker) {
		try {
	        if (db == null) {
	            Log.e("AcquistoDAO.delete", "db is null");
	            return false;
	        }
	        SQLiteDatabase database = db.getWritableDatabase();
	        String where = "";
	        if ( ticker != null) {
		        where = " " + Ticker.CSZ_ID + " = "+ticker.getID();
	        } else {
		        long data = acquisto.getDateLong();
	        	where = ""+Acquisto.CSZ_DATE + " = "+data;
		        where += " AND " + Acquisto.CSZ_ID + " = "+acquisto.getId();
	        }
	        long id = database.delete(db.ACQUISTI_TABLE,where,null);
	        if ( id < 0) 
	        	return false;
	        return true;
		} catch(Exception e) {
			e.printStackTrace();
			Log.e("AcquistoDAO.delete", "Errore in delete");
			return false;
		}
	}

	public static boolean leggiAcquisti(MercatoDBOpenHelper db, int id, Lista ls) {
		try {
	        if (db == null) {
	            Log.e("AcquistiDAO", "db is null");
	            return false;
	        }
	        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
	        if ( ls == null)
	        	ls = new Lista(Acquisto.class);
	        ls.clear();
	        DMC dmc = new DMC(db);
	        String select = "select * from "+db.ACQUISTI_TABLE+" where "+db.KEY_ID_TICKER+"="+id;
	        select += " order by "+db.KEY_DATE;
	        if ( ! dmc.load(ls, select)) {
	        	return false;
	        }
	        return true;
		} catch(Exception e) {
			e.printStackTrace();
			Log.e("AcquistiDAO", "Errore in apertura cursore");
			return false;
		}		
	}
	
	
}
